let co = require('co')
let OSS = require('ali-oss')
let fs = require('fs-extra')
let path = require('path')
let fetch = require('node-fetch')

let locationPath = './dist'

let client = new OSS({
    region: 'oss-cn-beijing',
    //云账号AccessKey有所有API访问权限，建议遵循阿里云安全最佳实践，部署在服务端使用RAM子账号或STS，部署在客户端使用STS。
    accessKeyId: 'LTAIezLsyVN7PmWV',
    accessKeySecret: 'cUlZp67hn8DLzmxk9cCSMzUkM8omYj',
    bucket: 'more-page'
});

async function listBuckets() {
    try {
        let result = await client.listBuckets();
        console.log(result)
    } catch (err) {
        console.log(err)
    }
}
async function put(filename,file) {

    try {
        let result = await client.put(filename, file);
    } catch (e) {
        console.log(e);
    }
}
async function deleteFile(filePath) {
    try {
        let result = await client.delete(filePath);
        console.log(result);
    } catch (e) {
        console.log(e);
    }
}

function getFiles(dir, callback) {
    let fileArr = []
    fs.readdir(dir, function (err, files) {
        if (err) {
            return callback(err)
        } else {
            let i = 0;
            (function next() {
                let fileName = files[i++]
                if (!fileName) return callback(null, fileArr)
                let filedir = path.join(dir, fileName)
                // console.log(filedir)
                fs.stat(filedir, function (error, stats) {
                    console.log(stats && stats.isDirectory())
                    if (stats && stats.isDirectory()) {
                        getFiles(filedir, function (error, data) {
                            fileArr = fileArr.concat(data)
                            next()
                            console.log(fileArr)
                        })
                    } else {
                        fileArr.push(filedir)
                        next()
                    }
                })
            })()
        }
    })
}


getFiles(locationPath, function (err, data) {
    if (err) throw err
    data.map(file => {
        let filePath = file.replace('dist/', 'morePage/')
        console.log(filePath)
        put(filePath,file)
    })
})

// getFiles(locationPath, function (err, data) {
//     if (err) throw err
//     data.map(file => {
//         // let filePath = file.replace('dist/', 'morePage/')
//         // console.log(filePath)
//         deleteFile (file)
//     })
// })
